package com.CST.service.impl.cb;

import com.CST.common.api.ResultObject;
import com.CST.dto.cb.InSourceDTO;
import com.CST.entity.cb.BdEnumdetail;
import com.CST.entity.cb.CostDataInfo;
import com.CST.entity.cb.EtlAccount;
import com.CST.entity.cb.EtlAccvouch;
import com.CST.mapper.cst.cb.EtlAccountMapper;
import com.CST.mapper.cst.cb.EtlAccvouchMapper;
import com.CST.mapper.dataplatform.BdEnumdetailMapper;
import com.CST.service.cb.IEtlAccvouchService;
import com.CST.vo.cb.InSourceVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EtlAccvouchServiceImpl implements IEtlAccvouchService {
    
    @Autowired
    private EtlAccvouchMapper etlAccvouchMapper;

    @Autowired
    private EtlAccountMapper etlAccountMapper;

    @Autowired
    private BdEnumdetailMapper bdEnumdetailMapper;

    /**
     * 获取非HIS收入列表
     * @param inSourceDTO
     * @return
     */
    @Override
    public ResultObject listEtlAccvouchSource(InSourceDTO inSourceDTO) {
        List<InSourceVO> inSourceVOList =  etlAccvouchMapper.listEtlAccvouchSource(inSourceDTO);
        return ResultObject.successReturn(inSourceVOList);
    }

    @Override
    public ResultObject getIncomeEtlAccvouchSource(EtlAccvouch entity) {
        if (entity.getYear() == null || entity.getYear().isEmpty()) {
            return ResultObject.errorReturn("凭证数据未能获取年份,请联系管理员");
        }
        if (entity.getMonth() == null || entity.getMonth().isEmpty()) {
            return ResultObject.errorReturn("凭证数据未能获取月份,请联系管理员");
        }
        //取查询时间的年
        entity.setTableName("ETL_Accvouch_" + entity.getYear());
        List<EtlAccvouch> list = etlAccvouchMapper.getIncomeEtlAccvouchSource(entity);
        return ResultObject.successReturn(list);
    }

    @Override
    public ResultObject getCostEtlAccvouchSource(EtlAccvouch entity) {
        if (entity.getYear() == null || entity.getYear().isEmpty()) {
            return ResultObject.errorReturn("凭证数据未能获取年份,请联系管理员");
        }
        if (entity.getMonth() == null || entity.getMonth().isEmpty()) {
            return ResultObject.errorReturn("凭证数据未能获取月份,请联系管理员");
        }
        //取查询时间的年
        entity.setTableName("ETL_Accvouch_" + entity.getYear());
        List<CostDataInfo> list = etlAccvouchMapper.getCostEtlAccvouchSource(entity);
        return ResultObject.successReturn(list);
    }

    @Override
    public List<InSourceVO> geitNoHisSourece(InSourceDTO entity) {
        Integer year = entity.getYear();
        Integer month = entity.getMonth();
        LocalDate date = LocalDate.of(year, month, 1);
        LocalDate nextDate = date.minusMonths(-1);
        entity.setStarTime(date.toString());
        entity.setEndTime(nextDate.toString());
        List<InSourceVO> NoHisSourceList = etlAccvouchMapper.getNoHisSourece(entity);
        return NoHisSourceList;
    }

    @Override
    public List<String> getCheckIncome(EtlAccount entity) {
        List<String> match = new ArrayList<>();
        if("Income".equals(entity.getSysid()))
        {
            entity.setEdParentCode("RD_IncomeUFIDARelation");
            entity.setNum("4");
        }
        else if("Cost".equals(entity.getSysid()))
        {
            entity.setEdParentCode("RD_CostUFIDARelation");
            entity.setNum("5");
        }
//        拿到所有收入的会计科目条数
        List<EtlAccount> kuaijikemu= etlAccvouchMapper.getIncome(entity);
        List<String> Income = kuaijikemu.stream().map(t -> t.getAccountcode()).collect(Collectors.toList());
        //拿到收入--用友系统关系对应 /成本--用友系统关系对应，根据Sysid判断
        LambdaQueryWrapper<BdEnumdetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BdEnumdetail::getEdParentCode, entity.getEdParentCode());
        List<BdEnumdetail> bdEnumdetails = bdEnumdetailMapper.selectList(lambdaQueryWrapper);
        List<String> Income2 = bdEnumdetails.stream().map(t -> t.getEdExtraFirstCode()).collect(Collectors.toList());
        for (String s : Income) {
            if (!(Income2.contains(s))) {
                match.add(s);
            }
        }
        return match;
    }
}
